package org.ucas.faker.web.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.ucas.faker.web.utils.tech.PatentFeaturizeRegularRule;

public class ProvinceNormal {
	private static ProvinceNormal singleton = null;
	List<String> provs;
	Map<String,String> mprovs = null;
	public ProvinceNormal() {
		mprovs = new HashMap<>();
		// TODO Auto-generated constructor stub
		try {
			InputStream is = this.getClass().getClassLoader().getResourceAsStream("provname/city");
			
            InputStreamReader read = new InputStreamReader(is);//考虑到编码格式
            BufferedReader featureReader =  new BufferedReader(read);
            String lineTxt = null;
            while((lineTxt = featureReader.readLine()) != null){
            	if(lineTxt.charAt(0)==' ') continue;
            	String [] tmp = lineTxt.split(" ");
            	for(int i=1;i<tmp.length;i++){
            		if(tmp[i].length()<1) continue;
            		//System.out.println(tmp[i].substring(0, tmp[i].length()-1));
            		mprovs.put(tmp[i].substring(0, tmp[i].length()-1), tmp[i]);
            	}
            }
            read.close();
	    } catch (Exception e) {
	        System.out.println("Error reading feature_regular.rule");
	        e.printStackTrace();
	    }
	}

	public static ProvinceNormal getInstance(){
		if(singleton==null){
			singleton = new ProvinceNormal();
		}
		return singleton;
		
	}
	
	public String getNormalName(String province){
		if(province==null) return null;
		for(Entry<String, String> entry :mprovs.entrySet()){
			if(province.contains(entry.getKey())) return entry.getValue();
		}
		return null;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		ProvinceNormal pf = ProvinceNormal.getInstance();
		
		System.out.println(pf.getNormalName("湖北省"));
		System.out.println(pf.getNormalName("湖北"));
		System.out.println(pf.getNormalName("湖省某个村子"));
		System.out.println(pf.getNormalName("湖北省某个村子"));
		
	
		//pf.featurize();
//		/pf.test();
		//PatentFeaturizeRegularRule pf = PatentFeaturizeRegularRule.getInstance();
		//pf.displayResult(pf.featurize(abs,keywords));
		//System.out.println(abs);
		//pf.TestFromFile2("testcase10_1");
	}
	

}
